// pages/addPays/addPays.js
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const { mpserverless } = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date().getTime() - 10000000000,
    maxDate: new Date().getTime(),
    chooseIncome: false,   //选择收入
    chooseExpenditure: true,   //选择支出
    payType: '',              //选择支出或收入类型
    payTypeIcon: '',          //类型icon
    payDate: '',                  //选择的日期
    payMoney: '',                  //消费金额
    payRemark: '',                //消费备注
    showCalendar: false,       //是否展示日历选择
    isLoading: false,            //保存按钮是否处于加载状态
    expenditureList: [
      { Item_id: "早餐", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/d27fb341-9858-40cd-ae3f-d6623b682c5f.png", Item_Name: "早餐" },
      { Item_id: "午餐", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/232f9c0b-8886-4e17-a6db-ae902c6d90df.png", Item_Name: "午餐" },
      { Item_id: "晚餐", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/e8692393-2198-4720-ae36-d1e139ce6bec.png", Item_Name: "晚餐" },
      { Item_id: "交通", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/55aeb831-e917-4d3e-9f6d-5e82dfd3a1a4.png", Item_Name: "交通" },
      { Item_id: "奶茶", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/e0a77a73-333f-4842-90ad-b4018f36cf4b.png", Item_Name: "奶茶" },
      { Item_id: "网购", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/f941111d-0161-44c9-bfe5-6e49541cdfa1.png", Item_Name: "网购" },
      { Item_id: "麻将", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/6e662893-d78e-41be-a90f-2b0f6a5c11b7.png", Item_Name: "麻将" },
      { Item_id: "饮料", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/a8553647-e7ac-4a6e-8682-ec63d699d72d.png", Item_Name: "饮料" },
      { Item_id: "护肤", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/c25ab4b2-964c-46bb-803f-412ae9b9f7aa.png", Item_Name: "护肤" },
      { Item_id: "娱乐", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/a0316388-8171-4a05-b56c-3e5a7fa21200.png", Item_Name: "娱乐" },
      { Item_id: "水果", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/5b77282b-bdac-4872-b3d6-1a60d970a32f.png", Item_Name: "水果" },
      { Item_id: "零食", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/03ca211f-3b3f-4e44-9e8b-6cf00cfbb978.png", Item_Name: "零食" },
      { Item_id: "衣物", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/e01ea2b0-3661-4619-acda-da703372b447.png", Item_Name: "衣物" },
      { Item_id: "旅游", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/c60160ec-cc65-4207-80f2-627a62702ac8.png", Item_Name: "旅游" },
      { Item_id: "医疗", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/b4edae8d-2dfd-44d6-bb7a-234269a71830.png", Item_Name: "医疗" },
      { Item_id: "人情", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/f0b8f9c4-12ee-40ef-8446-0beccbd2c1f4.png", Item_Name: "人情" },
      { Item_id: "住宿", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/66d18303-a347-4d22-95ae-88c5cf66bb72.png", Item_Name: "住宿" },
      { Item_id: "通讯", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/b46c8e6d-fbef-4906-93bd-788b5b9b1773.png", Item_Name: "通讯" },
      { Item_id: "学习", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/562867b9-2d77-466f-9cfe-986771de45f9.png", Item_Name: "学习" },
      { Item_id: "会员", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/b9d8b977-27ef-494a-85ca-3dd8f548e4ca.png", Item_Name: "会员" },
      { Item_id: "投资", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/5a5c9d8b-dc12-4a4f-8ff7-7178d628c6df.png", Item_Name: "投资" },
      { Item_id: "乱七八糟", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/1697a751-3c86-42a5-8cd8-38e14562dcc4.png", Item_Name: "乱七八糟" },
    ],
    incomeList: [
      { Item_id: "薪资", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/aae22ada-bf4a-4f23-b2cd-33b71d9c645b.png", Item_Name: "薪资" },
      { Item_id: "麻将", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/ae142054-f6fe-4866-961b-f2787ec9552a.png", Item_Name: "麻将" },
      { Item_id: "人情", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/84894d1d-4a06-409b-83ff-dfe7ac8f6064.png", Item_Name: "人情" },
      { Item_id: "公积金", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/f6969498-ee4e-4942-8437-4c0c5c2f4cd2.png", Item_Name: "公积金" },
      { Item_id: "投资", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/e2aae883-24fc-4006-b79e-c7f6857d4474.png", Item_Name: "投资" },
      { Item_id: "意外之财", Item_icon: "https://mp-f3b68706-562e-4e88-9436-5e8c05efc576.cdn.bspapp.com/cloudstorage/11df6d35-6733-44d7-8a2c-2831ecf513a8.png", Item_Name: "意外之财" },
    ]
  },

  //日期选择函数
  onConfirm(event) {
    this.setData({
      showCalendar: false,
      payDate: this.formatDate(event.detail)
    });
  },
  //类型选择函数
  selectApply: function (e) {
    let id = e.currentTarget.dataset.id;
    let icon = e.currentTarget.dataset.icon;
    this.setData({
      payType: id,
      payTypeIcon: icon
    })
  },
  incomeBtnClick(e) {
    this.setData({
      chooseIncome: true,
      chooseExpenditure: false
    })
  },
  expenditureBtnClick(e) {
    this.setData({
      chooseIncome: false,
      chooseExpenditure: true
    })
  },
  chooseType(e) {
    this.setData({
      btnClass: 'info'
    })
  },

  //金额以及备注的输入
  changePayMoney(e) {
    let money = e.detail.value;
    const exp = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    exp.test(money);
    if (money == '') { }
    else if (Number(money) == 0) {
      this.setData({
        payMoney: ''
      })
      Dialog.alert({
        message: '你小子，请输入正确的金额!!',
        confirmButtonText: '错了',
        theme: 'round-button',
      });
    }
    else if (!exp.test(money)) {
      this.setData({
        payMoney: ''
      })
      Dialog.alert({
        message: '你小子，请输入正确的金额!!',
        confirmButtonText: '错了',
        theme: 'round-button',
      });
    } else {
      this.setData({
        payMoney: money
      });
    }
  },
  changePayRemark(e) {
    this.setData({ payRemark: e.detail });
  },
  //日历的开关函数
  onDisplay() {
    this.setData({ showCalendar: true });
  },
  onClose() {
    this.setData({ showCalendar: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  formatDateStamp(date) {
    return new Date(date).getTime();
  },
  //提交表单
  saveData() {
    if (this.data.payMoney == '') {
      wx.showToast({
        title: '请填写金额！',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
    } else if (this.data.payType == '') {
      wx.showToast({
        title: '请选择类型！',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
    } else {
      this.setData({ isLoading: true });
      //转化数字
      let num = new Number(this.data.payMoney);
      let money = num.toFixed(2);
      const openid = getApp().globalData.openId;
      mpserverless.db.collection('account').insertOne({
        openId: openid,
        payDate: this.data.payDate,
        payDateStamp: this.formatDateStamp(this.data.payDate),
        detailInfo: {
          isPay: this.data.chooseExpenditure,
          expenditureType: this.data.payType,
          remark: this.data.payRemark,
          payMoney: money,
          payTypeIcon:this.data.payTypeIcon
        }
      })
        .then(res => {
          if (res.success == true) {
            Toast.success('记下来啦！');
            getApp().globalData.isFreash = true;
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }, 1000);
          } else {
            Toast.fail('出现问题！');
          }
        })
        .catch(console.error);
    }

  },
  //重置表单
  formReset() {
    this.setData({
      payType: '',
      payMoney: '',
      payRemark: '',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      payDate: this.formatDate(new Date()),
    });
  },
})